package com.mycjj.android.fragment;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.mycjj.android.R;
import com.mycjj.android.activity.My_NewsDetailActivity;
import com.mycjj.android.adapter.MainTabAdapter;
import com.mycjj.android.bean.ArticleListBaseBean;
import com.mycjj.android.bean.ArticleListBaseBean.ArticleListBean.Article;
import com.mycjj.android.http.ProjectHttpRequestImpl;
import com.mycjj.android.http.ProjectHttpRequestInterface;
import com.mycjj.android.pullrefresh.PullToRefreshBase;
import com.mycjj.android.pullrefresh.PullToRefreshListView;
import com.mycjj.android.pullrefresh.PullToRefreshBase.Mode;
import com.mycjj.android.pullrefresh.PullToRefreshBase.OnRefreshListener2;
import com.mycjj.android.util.Preferences;

import de.greenrobot.event.EventBus;

/**
 * 后服务
 * @author mark
 *
 */
public class MainServiceFragment extends BaseFragment{
	public static MainServiceFragment newInstance(String type) {
		MainServiceFragment fragment = new MainServiceFragment();
		Bundle bundle = new Bundle();
		bundle.putString("type", type);
		fragment.setArguments(bundle);
        return fragment;
    }
	private View rootView;
	private PullToRefreshListView mListView;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		rootView = inflater.inflate(R.layout.fragment_main_new_car, null);
		mListView = (PullToRefreshListView)rootView.findViewById(R.id.lv_pull_refresh_list);
		mListView.setMode(Mode.BOTH);
		mListView.setOnRefreshListener(new OnRefreshListener2<ListView>() {  
  
            @Override  
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {  
                Toast.makeText(getActivity(), "下拉刷新", Toast.LENGTH_LONG).show(); 
                initData(1,false);
            }  
  
            @Override  
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {  
                if(isHasNextData){//有第二页数据
                	index++;
                	initData(index,false);
                }else{
                	Toast.makeText(getActivity(), "没有更多的数据需要显示!", Toast.LENGTH_LONG).show();
                	mListView.onRefreshComplete();
                }
            }  
          
        });
		return rootView;
	}
	private boolean isHasNextData;
	private int index = 1;
	private static int pageCount = 10;//每页显示的条数
	private MainTabAdapter adapter;
	private String type;
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		Bundle bundle = getArguments();
		if(bundle!=null){
			type = bundle.getString("type");
		}
		adapter = new MainTabAdapter(getActivity());
		mListView.setAdapter(adapter);
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
				Article article = articleList.get(position);
				Intent intent = new Intent(getActivity(),My_NewsDetailActivity.class);
				intent.putExtra("id", article.getId());
				startActivity(intent);
			}
		});
		Log.e("mark", "type:"+type);
		initData(1,false);
	}
	List<Article> articleList;
	private void initData(int index,boolean isShow){
		
		requestInterface = new ProjectHttpRequestImpl(getActivity(), isShow);
		RequestParams params = new RequestParams();
		params.put("cid", type);
		params.put("client_id",ProjectHttpRequestInterface.client_id_key);
		params.put("token", Preferences.getSharedPreferences(getActivity(), "user_token", ""));
		params.put("offset", "0");//忽略多少条
		params.put("limit", String.valueOf(index * pageCount));//默认获取10条
		requestInterface.requestSystemArticleListWithDoneHandler("", params, new AsyncHttpResponseHandler(){
			@Override
			public void onSuccess(String content) {
				ArticleListBaseBean baseBean = dataInstance.getArticleListBaseBean();
				if (baseBean != null) {
					if (baseBean.getCode() == 200) {
						if(baseBean.getData() != null){
							isHasNextData = baseBean.getData().isHas_next_page();
							articleList = baseBean.getData().getList();
							adapter.setList(articleList);
						}else{
							Log.e("mark", "ArticleListBaseBean.getData() is null!");
						}
					} else {
						Toast.makeText(getActivity(), baseBean.getMsg(), Toast.LENGTH_SHORT).show();
					}
				} else {
					Log.e("mark", "ArticlecClassBaseBean is null!");
				}
				mListView.onRefreshComplete();
			}
		});
	}
	
//	@Override
//	public boolean onTouch(View v, MotionEvent event) {
//		Log.e("mark", "MainActivityFragment onTouch..."+event.getAction());
//		if(event.getAction() == MotionEvent.ACTION_UP){
//			Bundle bundle = new Bundle();
//			bundle.putInt("type", 0);
//			EventBus.getDefault().post(bundle);
//		}
//		return false;
//	}
}
